package com.jason.kotlin.gank.viewmodel

import android.app.Application
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.jason.kotlin.gank.model.GankBannerBean
import com.jason.kotlin.gank.model.GankDataBean
import com.jason.kotlin.gank.repository.GankApi
import com.jason.kotlin.gank.repository.GankApiImpl
import com.jason.library.common.base.BaseViewModel
import com.jason.library.common.base.DataWrapper
import kotlinx.coroutines.launch
import java.lang.Exception

class GankHomeViewModel(application: Application) : BaseViewModel(application) {

    val bannerData = MutableLiveData<DataWrapper<List<GankBannerBean>>>()


    val gankData = MutableLiveData<DataWrapper<List<GankDataBean>>>()

    private var gankApi : GankApi = GankApiImpl()

    private var curPage: Int = 0

    fun loadData() {

        curPage = 0
        viewModelScope.launch {
            showLoading()
            try {
                val bannerResponse = gankApi.loadBanners()
                val gankResponse = gankApi.loadWeekHotGanHuoWithViews()

                bannerData.postValue(DataWrapper.success(bannerResponse.data))
                gankData.postValue(DataWrapper.success(gankResponse.data))

                showContent()
            }catch (e: Exception) {
                showError(e.toString())
            }
        }
    }

}